package com.qf.test;

import com.qf.util.EsUtil;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;

import javax.naming.directory.SearchResult;
import java.util.Map;

public class testSimpSearch {
    //获取和es服务器连接对象
    private RestHighLevelClient client = EsUtil.getClient();

    //指定查询索引的名字
    private static final String index ="book";

    //------------根据term词元查询, 也就是根据单词查询--------

    @Test
    public void testTermQuery() throws Exception{

        //1. 创建请求对象
        SearchRequest request = new SearchRequest(index);

        //2. 创建查询条件对象
        SearchSourceBuilder builder = new SearchSourceBuilder();
        //3. 指定查询条件
        //指定从第几条开始查询
        builder.from(0);


        //指定每页查询多少条数据
        builder.size(5);

        //指定查询的域名和查询的单词
        builder.query(QueryBuilders.termQuery("name","鬼吹灯"));

        //4. 将查询条件对象放入请求对象中
        request.source(builder);
        //5. 执行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        //6. 返回结果
        SearchHits hits = response.getHits();

        //查询到的总条数
        long count = hits.getTotalHits().value;

        //遍历查询到的结果集
        for (SearchHit hit : hits) {
            //获取一条数据
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println("==========---"+sourceAsMap);
        }
        System.out.println("count"+count);
    }
    //----------根据多个词元查询-----------
    @Test
    public void testTermsQuery() throws Exception{

        //1. 创建请求对象
        SearchRequest request = new SearchRequest(index);

        //2. 创建查询条件对象
        SearchSourceBuilder builder = new SearchSourceBuilder();
        //3. 指定查询条件
        //指定从第几条开始查询
        builder.from(0);


        //指定每页查询多少条数据
        builder.size(5);

        //指定查询的域名和查询的单词
        builder.query(QueryBuilders.termsQuery("name","鬼吹灯","金瓶梅"));

        //4. 将查询条件对象放入请求对象中
        request.source(builder);
        //5. 执行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        //6. 返回结果
        SearchHits hits = response.getHits();

        //查询到的总条数
        long count = hits.getTotalHits().value;

        //遍历查询到的结果集
        for (SearchHit hit : hits) {
            //获取一条数据
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println("==========---"+sourceAsMap);
        }
        System.out.println("count ="+count);
    }
    //----------查看es索引中的所有数据-----------
    @Test
    public void testMatchAll() throws Exception{

        //1. 创建请求对象
        SearchRequest request = new SearchRequest(index);

        //2. 创建查询条件对象
        SearchSourceBuilder builder = new SearchSourceBuilder();
        //3. 指定查询条件
        //指定从第几条开始查询
        builder.from(0);


        //指定每页查询多少条数据
        builder.size(5);

        //查询所有
        builder.query(QueryBuilders.matchAllQuery());

        //4. 将查询条件对象放入请求对象中
        request.source(builder);
        //5. 执行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        //6. 返回结果
        SearchHits hits = response.getHits();

        //查询到的总条数
        long count = hits.getTotalHits().value;

        //遍历查询到的结果集
        for (SearchHit hit : hits) {
            //获取一条数据
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println("==========---"+sourceAsMap);
        }
        System.out.println("count ="+count);
    }
    //---------------模糊查询----------------
    @Test
    public void testMatchQuery() throws Exception{

        //1. 创建请求对象
        SearchRequest request = new SearchRequest(index);

        //2. 创建查询条件对象
        SearchSourceBuilder builder = new SearchSourceBuilder();
        //3. 指定查询条件
        //指定从第几条开始查询
        builder.from(0);


        //指定每页查询多少条数据
        builder.size(5);

        //设置查询条件
        builder.query(QueryBuilders.matchQuery("name","鬼吹灯"));

        //4. 将查询条件对象放入请求对象中
        request.source(builder);
        //5. 执行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        //6. 返回结果
        SearchHits hits = response.getHits();

        //查询到的总条数
        long count = hits.getTotalHits().value;

        //遍历查询到的结果集
        for (SearchHit hit : hits) {
            //获取一条数据
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println("==========---"+sourceAsMap);
        }
        System.out.println("count ="+count);
    }


    // 根据多个关键词查询, 并且可以设置多个关键词的关系, 是and还是or
    @Test
    public void testBooleanMatchQuery() throws Exception{

        //1. 创建请求对象
        SearchRequest request = new SearchRequest(index);

        //2. 创建查询条件对象
        SearchSourceBuilder builder = new SearchSourceBuilder();
        //3. 指定查询条件
        //指定从第几条开始查询
        builder.from(0);


        //指定每页查询多少条数据
        builder.size(5);

        //设置查询条件
        builder.query(QueryBuilders.matchQuery("desc","java 李瓶").operator(Operator.OR));//.operator(Operator.AND)


        //4. 将查询条件对象放入请求对象中
        request.source(builder);
        //5. 执行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        //6. 返回结果
        SearchHits hits = response.getHits();

        //查询到的总条数
        long count = hits.getTotalHits().value;

        //遍历查询到的结果集
        for (SearchHit hit : hits) {
            //获取一条数据
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println("==========---"+sourceAsMap);
        }
        System.out.println("count ="+count);
    }
    //从多个域中进行查询, 也就是从多个字段中查询
    @Test
    public void testMultiMatchQuery() throws Exception{

        //1. 创建请求对象
        SearchRequest request = new SearchRequest(index);

        //2. 创建查询条件对象
        SearchSourceBuilder builder = new SearchSourceBuilder();
        //3. 指定查询条件
        //指定从第几条开始查询
        builder.from(0);


        //指定每页查询多少条数据
        builder.size(5);

        //设置查询条件
        builder.query(QueryBuilders.multiMatchQuery("java","name","desc"));


        //4. 将查询条件对象放入请求对象中
        request.source(builder);
        //5. 执行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        //6. 返回结果
        SearchHits hits = response.getHits();

        //查询到的总条数
        long count = hits.getTotalHits().value;

        //遍历查询到的结果集
        for (SearchHit hit : hits) {
            //获取一条数据
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println("==========---"+sourceAsMap);
        }
        System.out.println("count ="+count);
    }
//根据主键Id查询
    @Test
    public void testFindById() throws Exception{
        //1. 创建请求对象
        GetRequest request = new GetRequest(index, "1");

        //2.执行查询
        GetResponse response = client.get(request, RequestOptions.DEFAULT);

        //3.输出结果
        System.out.println(response.getSourceAsMap());

    }
    //----------根据多个id查询---------
    @Test
    public void testFindByIds() throws Exception{

        //1. 创建请求对象
        SearchRequest request = new SearchRequest(index);

        //2. 创建查询条件对象
        SearchSourceBuilder builder = new SearchSourceBuilder();
        //3. 指定查询条件
        //指定从第几条开始查询
        builder.from(0);


        //指定每页查询多少条数据
        builder.size(5);

        //设置查询条件
        builder.query(QueryBuilders.idsQuery().addIds("1","2","3"));


        //4. 将查询条件对象放入请求对象中
        request.source(builder);
        //5. 执行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        //6. 返回结果
        SearchHits hits = response.getHits();

        //查询到的总条数
        long count = hits.getTotalHits().value;

        //遍历查询到的结果集
        for (SearchHit hit : hits) {
            //获取一条数据
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println("==========---"+sourceAsMap);
        }
        System.out.println("count ="+count);
    }
    //根据字数范围查询
    @Test
    public void testFindByRange() throws Exception{

        //1. 创建请求对象
        SearchRequest request = new SearchRequest(index);

        //2. 创建查询条件对象
        SearchSourceBuilder builder = new SearchSourceBuilder();
        //3. 指定查询条件
        //指定从第几条开始查询
        builder.from(0);


        //指定每页查询多少条数据
        builder.size(5);

        //设置查询条件
        builder.query(QueryBuilders.rangeQuery("count").gte(200000).lte(5000000));


        //4. 将查询条件对象放入请求对象中
        request.source(builder);
        //5. 执行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        //6. 返回结果
        SearchHits hits = response.getHits();

        //查询到的总条数
        long count = hits.getTotalHits().value;

        //遍历查询到的结果集
        for (SearchHit hit : hits) {
            //获取一条数据
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println("==========---"+sourceAsMap);
        }
        System.out.println("count ="+count);
    }
}
